fix non-copyable derived dependency passed as base-class reference (issue #467)#679
Open
PavelGuzenfeld wants to merge 1 commit into
Open
Conversation
…ssue boost-ext#467) When an action or guard takes Base& but the sm<> ctor receives Derived& (e.g. NiceMock<T>, any type with a deleted copy constructor), the pool init copy-constructor called try_get<Base>() which found nothing in the source pool (which held Derived&) and fell back to missing_ctor_parameter<Base>. missing_ctor_parameter tried to default-construct Base and bind it to Base& — which fails (can't bind a reference to a temporary). Root cause: try_get had no covariant overloads; lookup was exact-type only. Fix: add two new try_get overloads constrained on is_base_of<T,D>: try_get<T>(pool_type<D&>*) -> T& try_get<T>(pool_type<const D&>*) -> const T& Both return the stored D& / const D& value, which implicitly converts to the base-class reference T& / const T&. is_same<T,D> is excluded to prevent ambiguity with the existing exact-match overloads. Regression test: non_copyable_derived_dep_as_base_ref in test/ft/dependencies.cpp — passes derived467 (inherits base467, deleted copy ctor) to sm<c467> whose action takes base467&.
9ac2e3b to
449dbd7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #467.
Root cause
When an action takes
Base&but thesm<>constructor receivesDerived&(e.g.NiceMock<T>, or any type with a deleted copy constructor), the pool init copy-constructor calledtry_get<Base>(&source_pool). The source pool heldpool_type<Derived&>, notpool_type<Base&>, so the exact-match overload was not viable. The fallbacktry_get(...)returnedmissing_ctor_parameter<Base>, which tried to default-constructBaseand bind it toBase&— which fails (cannot bind a reference to a temporary).This regression was introduced in v1.1.4 when
missing_ctor_parameterwas added.Fix
Add two covariant
try_getoverloads constrained onis_base_of<T,D> && !is_same<T,D>:Both return the stored
D&/const D&value, which implicitly upcasts toT&/const T&. The!is_same<T,D>constraint prevents ambiguity with the existing exact-match overloads.Test
Added
non_copyable_derived_dep_as_base_reftotest/ft/dependencies.cpp: passesderived467(inheritsbase467, copy ctor deleted) tosm<c467>whose action takesbase467&.